using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using sqengine.Engine.Common;
using sqengine.PhysicObjects;


namespace sqengine.Components
{
    public class TargetObj
    {
        public string text;
        public Vector2 position;
        public int life = 0;
        public int max_life = 1000;

    }

    public class TargetManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Camera _camera;
        SpriteBatch spriteBatch;
        Game _game;
        Vector3 _target_pos;
        SpriteFont _font;
        string _text;
        bool _enabled = false;

        List<PK3_Model> _botlist = new List<PK3_Model>();
        List<TargetObj> _targets = new List<TargetObj>();

        public TargetManager(Game game, Camera camera, SpriteFont font)
            : base(game)
        {
            _game = game;
            _camera = camera;
            _font = font;
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
        }

        public List<PK3_Model> Targets { get { return _botlist; } set { _botlist = value; } }


        public override void Initialize()
        {
            base.Initialize();
        }

        public void Start()
        {
            _enabled = true;
        }

        public void Stop()
        {
            _enabled = false;
        }



        public override void Update(GameTime gameTime)
        {

            if (_enabled)
            {
                for (int i = 0; i < _botlist.Count; i++)
                {
                    TargetObj _toj = new TargetObj();

                    _toj.life = _botlist[i].BotPlayer.Life;
                    _toj.max_life = _botlist[i].BotPlayer.MaxLife;
                    _toj.position = Get2DCoords(_botlist[i].PhysicsBody.Position, _game.Window);
                    _toj.text = _botlist[i].BotPlayer.Name;

                    if (_targets.IndexOf(_toj) >= 0)
                    {
                        _targets[_targets.IndexOf(_toj)] = _toj;
                    }
                    else
                    {
                        _targets.Add(_toj);
                    }
                }
            }

            base.Update(gameTime);
        }


        public void EnableTargets(bool state)
        {
            if (state)
            {
                _enabled = !_enabled;
                ((sqengine)_game).AddMessage(String.Format("Target mode enable : {0}", _enabled), Color.White, 1000, "");
            }



        }
        public override void Draw(GameTime gameTime)
        {
            if ((_enabled) & (!((sqengine)_game).isIntroPlaying))
            {
                if (_target_pos != Vector3.Zero)
                {
                    WriteTarget(_text);
                }
            }

            base.Draw(gameTime);
        }

        public void UpdateTargetPos(Vector3 position, string text)
        {
            _target_pos = position;
            _text = text;
        }


        private Vector2 Get2DCoords(Vector3 myPosition, GameWindow window)
        {
            Matrix ViewProjectionMatrix = _camera.View * _camera.Projection;

            Vector4 result4 = Vector4.Transform(myPosition, ViewProjectionMatrix);

            if (result4.W <= 0)
                return new Vector2(_camera.GameViewport.Width, 0);

            Vector3 result = new Vector3(result4.X / result4.W, result4.Y / result4.W, result4.Z / result4.W);

            Vector2 retVal = new Vector2((int)Math.Round(+result.X * (window.ClientBounds.Width / 2)) + (window.ClientBounds.Width / 2), (int)Math.Round(-result.Y * (window.ClientBounds.Height / 2)) + (window.ClientBounds.Height / 2));
            return retVal;
        }

        public void WriteTarget(string txt)
        {
            Vector2 pos = Get2DCoords(_target_pos, _game.Window);
            pos.Y = pos.Y - 60;
            spriteBatch.Begin();
            for (int i = 0; i < _botlist.Count; i++)
            {

                spriteBatch.DrawString(_font, _botlist[i].BotPlayer.Name + "\n" + _botlist[i].BotPlayer.Life + " / " + _botlist[i].BotPlayer.MaxLife, Get2DCoords(_botlist[i].PhysicsBody.Position, _game.Window), Color.White);
            }
            spriteBatch.End();
        }
    }
}